﻿using VIFON.BLL;
using VIFON.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;

namespace VIFON
{
    public partial class frmCustomers : Telerik.WinControls.UI.RadForm
    {
        public frmCustomers()
        {
            InitializeComponent();
        }
        BLCustomer blCustomer = new BLCustomer();
        BLPaymentType blPaymentType = new BLPaymentType();
        BLRegion blRegion = new BLRegion();
        public static int CustomerId = int.MinValue;
        private static VIFON.BLL.Utilities.ControlMode CurrentMode = VIFON.BLL.Utilities.ControlMode.Normal;
        public string keyWord = string.Empty;

        private void frmCustomers_Load(object sender, EventArgs e)
        {
            ShowToolbar();
            LoadPaymentTypes();
            LoadRegions();
            LoadList();
        }

        private void LoadRegions()
        {
            ddlRegion.DataSource = BLRegion.GetRegions();
            ddlRegion.DisplayMember = "Name";
            ddlRegion.ValueMember = "Id";
        }

        private void LoadList()
        {
            grvCustomers.Rows.Clear();
            var lst = blCustomer.GetCustomers(keyWord);
            if (lst != null)
            {
                foreach (var l in lst)
                {
                    grvCustomers.Rows.Add(l.Id, l.CustomerName, l.TaxCode, l.Address, l.PaymentType1.Name, l.AccountNumber, l.Discount, l.Region1.Name);
                }
            }
            grvCustomers.Refresh();
            this.Refresh();
        }

        private void LoadPaymentTypes()
        {
            ddlPaymentType.DataSource = blPaymentType.GetPaymentTypes();
            ddlPaymentType.DisplayMember = "Name";
            ddlPaymentType.ValueMember = "Id";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            CurrentMode = VIFON.BLL.Utilities.ControlMode.New;
            ShowToolbar();
        }

        #region Tool bar
        private void ShowToolbar()
        {
            switch (CurrentMode)
            {
                case VIFON.BLL.Utilities.ControlMode.Normal:
                    txtAddress.Enabled = txtAccountNumber.Enabled = txtName.Enabled = txtTaxCode.Enabled = ddlPaymentType.Enabled = ddlRegion.Enabled = txtDiscount.Enabled = false;
                    btnCancel.Visible = false;
                    btnDelete.Visible = true;
                    btnAdd.Visible = true;
                    btnSave.Visible = false;
                    btnEdit.Visible = true;
                    txtName.Focus();
                    break;
                case VIFON.BLL.Utilities.ControlMode.New:
                    txtAddress.Enabled = txtAccountNumber.Enabled = txtName.Enabled = txtTaxCode.Enabled = ddlPaymentType.Enabled = ddlRegion.Enabled = txtDiscount.Enabled = true;
                    btnCancel.Visible = true;
                    btnDelete.Visible = false;
                    btnEdit.Visible = false;
                    btnAdd.Visible = false;
                    btnSave.Visible = true;
                    txtName.Enabled = true;
                    txtName.Focus();
                    ClearText();
                    break;
                case VIFON.BLL.Utilities.ControlMode.Edit:
                    txtAddress.Enabled = txtAccountNumber.Enabled = txtName.Enabled = txtTaxCode.Enabled = ddlPaymentType.Enabled = ddlRegion.Enabled = txtDiscount.Enabled = true;
                    btnCancel.Visible = true;
                    btnDelete.Visible = false;
                    btnEdit.Visible = false;
                    btnAdd.Visible = false;
                    txtName.Focus();
                    btnSave.Visible = true;
                    break;
            }
        }
        #endregion

        private void ClearText()
        {
            txtName.Text = string.Empty;
            txtTaxCode.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtAccountNumber.Text = string.Empty;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string customerName = txtName.Text;
            string taxCode = txtTaxCode.Text;
            string address = txtAddress.Text;
            string discount = txtDiscount.Text;

            int paymentType = (int)ddlPaymentType.SelectedValue;
            int region = (int)ddlRegion.SelectedValue;
            float disCount = int.MinValue;
            string accountNumber = txtAccountNumber.Text;

            if (string.IsNullOrEmpty(customerName) || string.IsNullOrEmpty(discount) || string.IsNullOrEmpty(address) || string.IsNullOrEmpty(discount))
            {
                MessageBox.Show(Common.IsNullOrEmpty, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!float.TryParse(discount, out disCount))
            {
                MessageBox.Show(Common.DiscountInValid, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Customer boCustomer = new Customer
            {
                Id = CustomerId,
                AccountNumber = accountNumber,
                Address = address,
                CustomerName = customerName,
                PaymentType = (int)ddlPaymentType.SelectedValue,
                TaxCode = taxCode,
                Discount = disCount,
                Region = region,
            };

            //Chế độ chỉnh sửa
            if (CurrentMode == VIFON.BLL.Utilities.ControlMode.New)
            {
                if (blCustomer.Insert(boCustomer))
                {
                    CurrentMode = VIFON.BLL.Utilities.ControlMode.Normal;
                    ShowToolbar();
                    ClearText();
                    MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (blCustomer.Update(boCustomer))
                {
                    CurrentMode = VIFON.BLL.Utilities.ControlMode.Normal;
                    ShowToolbar();
                    ClearText();
                    MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            LoadList();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CurrentMode = VIFON.BLL.Utilities.ControlMode.Normal;
            ClearText();
            ShowToolbar();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            CurrentMode = VIFON.BLL.Utilities.ControlMode.Edit;
            ShowToolbar();
        }

        private void grvCustomers_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            try
            {
                if (CurrentMode != BLL.Utilities.ControlMode.New)
                {
                    int rowindex = grvCustomers.CurrentCell.RowIndex;
                    CustomerId = int.Parse(grvCustomers.Rows[rowindex].Cells[0].Value.ToString());
                    txtName.Text = grvCustomers.Rows[rowindex].Cells[1].Value.ToString();
                    txtTaxCode.Text = grvCustomers.Rows[rowindex].Cells[2].Value.ToString();
                    txtAddress.Text = grvCustomers.Rows[rowindex].Cells[3].Value.ToString();
                    ddlPaymentType.Text = grvCustomers.Rows[rowindex].Cells[4].Value.ToString();
                    txtAccountNumber.Text = grvCustomers.Rows[rowindex].Cells[5].Value.ToString();
                    txtDiscount.Text = grvCustomers.Rows[rowindex].Cells[6].Value.ToString();
                    ddlRegion.Text = grvCustomers.Rows[rowindex].Cells[7].Value.ToString();
                }
            }
            catch { }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (DialogResult.Yes == MessageBox.Show(Common.ConfirmDelete, Common.MessageTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                {
                    BLBill blBill = new BLBill();
                    blBill.Delete(CustomerId);
                    if (blCustomer.Delete(CustomerId))
                        MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    LoadList();
                }
            }
            catch
            {
                MessageBox.Show(Common.SelectRowData, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            keyWord = txtKeyWord.Text;
            LoadList();
        }
    }
}
